package CommunityController;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.HashMap;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import common.PagingManager;

/**
 * Servlet implementation class FreeBoardAction
 */
public class FreeBoardAction extends HttpServlet {
	private static final long serialVersionUID = 1L;
	private static final String sessionID  = null;
	private HttpSession session = null;
    FreeBoardDAO  freeBoardDAO =new  FreeBoardDAO ();
    /**
     * @see HttpServlet#HttpServlet()
     */
    public FreeBoardAction() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		doPost(request,response);
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	private int currentPage=1;
	
 	private PagingManager paging;


	

	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		request.setCharacterEncoding("UTF-8");
		response.setCharacterEncoding("UTF-8");
		String action=request.getParameter("action");
		FreeBoardBean freeBoardBean=null;
		RequestDispatcher view = null;
		PrintWriter out = response.getWriter();
		if(action.equals("cm_free_list_main")){
			ArrayList list =freeBoardDAO.cm_free_list_main();
			request.setAttribute("list", list);
			
			request.getRequestDispatcher("/Main/OldMain/home_board1.jsp").forward(request, response);
		}
		else if(action.equals("cm_free_list")){
				System.out.println("여기는 리스트");
				String mem_nick=request.getParameter("nick");
				int level=freeBoardDAO.young_level(mem_nick);
			   String searchword=request.getParameter("searchword");
			   String search=request.getParameter("search");
			   if(request.getParameter("currentPage")!=null && !"".equals(request.getParameter("currentPage"))){
					currentPage=Integer.parseInt(request.getParameter("currentPage"));
				}
				HashMap param=new HashMap();
				param = new HashMap<String, Object>();
				param.clear();
				param.put("pageSize", 15 );
				param.put("currentPage", currentPage );
				param.put("pageGroupSize", 5 );
				
				param.put("searchword", searchword);
				param.put("search",search);
				
				
				int pageNo = ((Integer)param.get("currentPage") -1) * (Integer)param.get("pageSize");
				int endNum = pageNo + (Integer)param.get("pageSize");
				param.put( "startNum", pageNo );
				
				ArrayList list =freeBoardDAO.cm_free_list(param);
				int totalCount = freeBoardDAO.cfTotalCount(param);
				param.put("totalCount", totalCount);
				paging=new common.PagingManager();
				paging.setPageNo(param);
				
				
				request.setAttribute("list", list);
				request.setAttribute("paging", paging.getReturnHtml());
				request.setAttribute("page","/community/FreeBoard/CM_Free_List");
				
				if(level==2){
					
					view = request.getRequestDispatcher("/Main/YoungMain/Ym_Main.jsp");
					
				}else if(level==1){
					view = request.getRequestDispatcher("/Main/OldMain/Om_Main.jsp");	
				}else if(level==3){
					view = request.getRequestDispatcher("/Main/admin/Ad_Main.jsp");
				}
				view.forward(request, response);
				
		   }else if(action.equals("cm_free_read")){
			   String mem_nick=request.getParameter("nick");
				int level=freeBoardDAO.young_level(mem_nick);
			   System.out.println("aaa");
			   System.out.println("레벨몇?"+level);
			   System.out.println("아이디머?"+mem_nick);
			   int id=Integer.parseInt(request.getParameter("id"));
			   freeBoardBean=new FreeBoardBean();
			   freeBoardBean= freeBoardDAO.cm_free_read(id);
			   
			    session = request.getSession();
				session.setAttribute("freeBoardBean", freeBoardBean);
				request.setAttribute("page","/community/FreeBoard/CM_Free_Read");
				if(level==2){
					
					view = request.getRequestDispatcher("/Main/YoungMain/Ym_Main.jsp");
					
				}else if(level==1){
					view = request.getRequestDispatcher("/Main/OldMain/Om_Main.jsp");	
				}else if(level==3){
					view = request.getRequestDispatcher("/Main/admin/Ad_Main.jsp");
				}
				view.forward(request, response);
		   }else if(action.equals("cm_free_write_form")){
			   String mem_nick=request.getParameter("nick");
				int level=freeBoardDAO.young_level(mem_nick);
				
				request.setAttribute("page","/community/FreeBoard/CM_Free_Write");
				if(level==2){
					
					view = request.getRequestDispatcher("/Main/YoungMain/Ym_Main.jsp");
					
				}else if(level==1){
					view = request.getRequestDispatcher("/Main/OldMain/Om_Main.jsp");	
				}else if(level==3){
					view = request.getRequestDispatcher("/Main/admin/Ad_Main.jsp");
				}
				view.forward(request, response);
			}else if(action.equals("cm_free_write")){
				String mem_nick=request.getParameter("nick");
				int level=freeBoardDAO.young_level(mem_nick);
			   freeBoardBean=new FreeBoardBean();
			   //freeBoardBean.setId(Integer.parseInt(request.getParameter("id")));
			   freeBoardBean.setTitle(request.getParameter("title"));
			   freeBoardBean.setContents(request.getParameter("contents"));
			   freeBoardBean.setNick(request.getParameter("nick"));
			   //freeBoardBean.setRef(Integer.parseInt(request.getParameter("ref")));
			   //freeBoardBean.setRe_step(Integer.parseInt(request.getParameter("re_step")));
			   freeBoardDAO.cm_free_write(freeBoardBean);
			   //response.sendRedirect("/FreeBoardAction.do?action=cm_free_list");
			    out.println( "<script>" );
				//out.println( "alert(\"완료.\");" );
				out.println( "location.href=\"/KOMORIUTA/FreeBoardAction.do?action=cm_free_list&nick="+mem_nick+"\";" );
				out.println( "</script>" ); 
			  // request.getRequestDispatcher("/FreeBoardAction.do?action=cm_free_list").forward(request, response);
		   }else if(action.equals("cm_free_modify_form")){
			   String mem_nick=request.getParameter("nick");
				int level=freeBoardDAO.young_level(mem_nick);
				int id=Integer.parseInt(request.getParameter("id"));
				freeBoardBean=freeBoardDAO.cm_free_read(id);
				
			    session = request.getSession();
				session.setAttribute("freeBoardBean", freeBoardBean);
				request.setAttribute("page","/community/FreeBoard/CM_Free_Modify");
				if(level==2){
					
					view = request.getRequestDispatcher("/Main/YoungMain/Ym_Main.jsp");
					
				}else if(level==1){
					view = request.getRequestDispatcher("/Main/OldMain/Om_Main.jsp");	
				}
				view.forward(request, response);
				//request.setAttribute("freeBoardBean", freeBoardBean);
				//request.getRequestDispatcher("/community/FreeBoard/CM_Free_Modify.jsp").forward(request, response);
			}else if(action.equals("cm_free_modify")){
				String mem_nick=request.getParameter("nick");
				int level=freeBoardDAO.young_level(mem_nick);
			   freeBoardBean=new FreeBoardBean();
				int id=Integer.parseInt(request.getParameter("id"));
				freeBoardBean.setId(id);
				freeBoardBean.setTitle(request.getParameter("title"));
				freeBoardBean.setContents(request.getParameter("contents"));
				freeBoardDAO.cm_free_modify(freeBoardBean);
				out.println( "<script>" );
				//out.println( "alert(\"완료.\");" );
				out.println( "location.href=\"/KOMORIUTA/FreeBoardAction.do?action=cm_free_list&nick="+mem_nick+"\";" );
				out.println( "</script>" ); 
				//request.getRequestDispatcher("/FreeBoardAction.do?action=cm_free_list").forward(request, response);
		   }else if(action.equals("cm_free_delete")){
			   String mem_nick=request.getParameter("nick");
				int level=freeBoardDAO.young_level(mem_nick);
			   int ref=Integer.parseInt(request.getParameter("ref"));
			   freeBoardDAO.cm_free_delete(ref);
			   out.println( "<script>" );
				//out.println( "alert(\"완료.\");" );
				out.println( "location.href=\"/KOMORIUTA/FreeBoardAction.do?action=cm_free_list&nick="+mem_nick+"\";");
				out.println( "</script>" ); 
			   //request.getRequestDispatcher("/FreeBoardAction.do?action=cm_free_list").forward(request, response);
		   
		   }else if(action.equals("cm_free_reple_form")){
			   String mem_nick=request.getParameter("nick");
				int level=freeBoardDAO.young_level(mem_nick);
			    //session = request.getSession();
				//session.setAttribute("freeBoardBean", freeBoardBean);
				request.setAttribute("page","/community/FreeBoard/CM_Free_Rwrite");
				if(level==2){
					
					view = request.getRequestDispatcher("/Main/YoungMain/Ym_Main.jsp");
					
				}else if(level==1){
					view = request.getRequestDispatcher("/Main/OldMain/Om_Main.jsp");	
				}else if(level==3){
					view = request.getRequestDispatcher("/Main/admin/Ad_Main.jsp");
				}
				view.forward(request, response);
		   }
		   else if(action.equals("cm_free_reple")){
			   String mem_nick=request.getParameter("nick");
				int level=freeBoardDAO.young_level(mem_nick);
			   freeBoardBean=new FreeBoardBean();
			   freeBoardBean.setId(Integer.parseInt(request.getParameter("id")));
			   freeBoardBean.setTitle(request.getParameter("title"));
			   freeBoardBean.setContents(request.getParameter("contents"));
			   freeBoardBean.setNick(request.getParameter("nick"));
			   freeBoardBean.setRef(Integer.parseInt(request.getParameter("ref")));
			   freeBoardBean.setRe_step(Integer.parseInt(request.getParameter("re_step")));
			   freeBoardBean.setRe_level(Integer.parseInt(request.getParameter("re_level")));
			   
			   freeBoardDAO.cm_free_reple(freeBoardBean);
			   out.println( "<script>" );
				//out.println( "alert(\"완료.\");" );
				out.println( "location.href=\"/KOMORIUTA/FreeBoardAction.do?action=cm_free_list&nick="+mem_nick+"\";" );
				out.println( "</script>" ); 
			   //request.getRequestDispatcher("/FreeBoardAction.do?action=cm_free_list").forward(request, response);
		   }
	}

}
